<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-input v-model="state.tableData.param.orderNumber" size="default" placeholder="请输入订单编号"
					style="max-width: 180px" clearable> </el-input>
				<el-date-picker v-model="state.tableData.dataTime" size="default" class="ml10" type="daterange"
					range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
				</el-date-picker>
				<el-button size="default" type="primary" class="ml10" @click="getTableData()">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" :index="indexMethod" label="序号" width="60" />
				<el-table-column prop="orderNumber" label="订单编号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="statue" label="订单状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="warning" v-if="scope.row.statue === 1">已支付</el-tag>
						<el-tag type="success" v-else-if="scope.row.statue === 2">已完成</el-tag>
						<el-tag type="info" v-else>待支付</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="price" label="支付价格" show-overflow-tooltip></el-table-column>
				<el-table-column prop="userPhon" label="用户电话号码" show-overflow-tooltip></el-table-column>
				<el-table-column prop="orderTime" label="订单时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="订单详情" width="100">
					<template #default="scope">
						<el-button  size="small" text type="primary" @click="onOpenAddRole(scope.row)"
							>查看</el-button
						>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button v-if="scope.row.statue == 1" type="success" :icon="Check" circle
							@click="onOpenEditRole(scope.row.orderId)" />
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
				:pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum"
				background v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper" :total="state.tableData.total">
			</el-pagination>
		</div>
		<RoleDialog ref="roleDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="order">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { orderApi } from '/@/api/order/index';
import { Check } from '@element-plus/icons-vue';

// 引入组件
const RoleDialog = defineAsyncComponent(() => import('/@/views/order/dialog.vue'));

// 定义变量内容
const roleDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		dataTime: [],
		param: {
			orderNumber: '',
			pageNum: 1,
			pageSize: 10,
			beganTime: null as string | null,
			endTime: null as string | null,
		},
	},
});

const formatDate = (dateTime: string | number | Date) => {
	const date = new Date(dateTime);
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');
	return `${year}-${month}-${day}`;
};

// 初始化表格数据
const getTableData = () => {
	// 动态设置时间范围参数
	if (state.tableData.dataTime && state.tableData.dataTime.length === 2) {
		state.tableData.param.beganTime = formatDate(state.tableData.dataTime[0]);
		state.tableData.param.endTime = formatDate(state.tableData.dataTime[1]);
	} else {
		state.tableData.param.beganTime = null;
		state.tableData.param.endTime = null;
	}

	orderApi().selectOrderPage(state.tableData.param).then(res => {
		state.tableData.loading = true;
		state.tableData.data = res.data.records;
		state.tableData.total = res.data.total;
		setTimeout(() => {
			state.tableData.loading = false;
		}, 500);
	});

};

// 自定义索引方法
const indexMethod = (index: number) => {
	return (state.tableData.param.pageNum - 1) * state.tableData.param.pageSize + index + 1;
};

// 打开新增角色弹窗
const onOpenAddRole = (row: Object) => {
	roleDialogRef.value.openDialog(row);
};

//完成订单
const onOpenEditRole = (orderId: number) => {
	return ElMessageBox.confirm('确定完成订单吗？', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		orderApi().complete(orderId).then(() => {
			ElMessage.success('订单已完成');
			getTableData();
		})

	})
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};

// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;

		.el-table {
			flex: 1;
		}
	}
}
</style>